import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/login&register/model/shared_user_info.dart';
import 'package:find_friend/pages/xspace/model/artclemodel.dart';
import 'package:find_friend/pages/xspace/model/school_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ShareWidget extends StatefulWidget {

  final ArticleModel articleModel;
  final ReplyModel replyModel;
  final Function() onRefreshData;

  const ShareWidget({Key key, this.articleModel, this.replyModel, this.onRefreshData}) : super(key: key);

  static const List<String> titles = [
    "站内好友",
    "微信",
    "朋友圈",
    "QQ",
    "QQ空间",
    "分享长图",
    "复制链接"
  ];
  static const List<String> imgs = [
      "assets/zhanneihaoyou.png",
      "assets/weixin.png",
      "assets/pengyouquan.png",
      "assets/qq.png",
      "assets/qqkongjian.png",
      "assets/fenxiangtup.png",
      "assets/fuzhilianjie.png"
  ];
  static const List<String> imgs2 = [
    "assets/fuzhiwenzi.png",
    "assets/jubao.png",
    "assets/sahnchu.png"
  ];
  static const List<String> titles2 = [
    "复制文字",
    "举报",
    "删除",
  ];

  @override
  _ShareWidgetState createState() => _ShareWidgetState();
}

class _ShareWidgetState extends State<ShareWidget> {

  bool isself = false;

  @override
  void initState() {
    super.initState();

    SharedUserInfo.instance().then((value) {
      if(widget.articleModel != null) {
        isself = value.mobile == widget.articleModel.mobile;
      } else {
        isself = value.mobile == widget.replyModel.mobile;
      }
      if(mounted) setState(() {});
    });
  }

  void _deleaction(int value) {
    switch (value) {
      case 0: // 复制文字
        _copyToClipborad();
        break;
      case 1: // 举报
        Navigator.pushNamed(context, "/msgreport", arguments: widget.articleModel != null ? widget.articleModel.mobile : widget.replyModel.mobile);
        break;
      case 2: // 删除
        widget.articleModel != null ? _requestArticleDelete() : _requestReplyDelete();
        break;
      default:
    }
    Navigator.of(context).pop();
  }

  void _requestArticleDelete() {
    Networktool.request(API.article + "/" + widget.articleModel.id.toString(), method: NETWORKHTTPMethod.DELETE, success: (data){
      if(data != null) {
        AlertTool.showToast("删除成功");
        if(widget.onRefreshData != null) widget.onRefreshData();
      }
    },
    fail: (msg) => AlertTool.showToast(msg));
  }

  void _requestReplyDelete() {
    Networktool.request(API.articlereply + "/" + widget.replyModel.id.toString(), method: NETWORKHTTPMethod.DELETE, success: (data){
      if(data != null) {
        AlertTool.showToast("删除成功");
        if(widget.onRefreshData != null) widget.onRefreshData();
      }
    },
    fail: (msg) => AlertTool.showToast(msg));
  }

  void _copyToClipborad() {
    String res;
    if(widget.articleModel != null) {
      if(widget.articleModel.content != null) {
        res = widget.articleModel.content;
      }
    } else {
      if(widget.replyModel.content != null) {
        res = widget.replyModel.content;
      }
    }
    if(res != null) {
      Clipboard.setData(ClipboardData(text: res)).then((value) => AlertTool.showToast("复制成功"));
    }
  }

  List<Widget> _getDele() {
    List<Widget> list2 = [];
    int length = (isself ? 3 : 2);
    for (var i = 0; i < length; i++) {
      Widget wid = InkWell(
        onTap: (){
          _deleaction(i);
        },
        child: Column(
          children: [
            Image.asset(ShareWidget.imgs2[i]),
            Spacer(),
            Text(ShareWidget.titles2[i], style: instanceTextStyle(10, APP_font_grey),)
          ],
        ),
      );
      list2.add(wid);
      if(i != ShareWidget.titles2.length - 1)list2.add(SizedBox(width: 30,));
    }
    return list2;
  }

  List<Widget> _createShare() {
    List<Widget> list = [];
    for (var i = 0; i < ShareWidget.titles.length; i++) {
      Widget wid = InkWell(
        onTap: (){
        },
        child: Column(
          children: [
            Image.asset(ShareWidget.imgs[i]),
            Spacer(),
            Text(ShareWidget.titles[i], style: instanceTextStyle(10, APP_font_grey),)
          ],
        ),
      );
      list.add(wid);
      if(i != ShareWidget.titles.length - 1)list.add(SizedBox(width: 30,));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;
    final bottom = MediaQuery.of(context).padding.bottom;
        return SafeArea(
          bottom: false,
          child: Container(
            color: Colors.white,
            height: 190 + bottom,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                  width: size.width,
                  height: 70,
                  child: ListView(
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    scrollDirection: Axis.horizontal,
                    children: _createShare(),
                  ),
                ),
                Divider(height: 0.5,color: APP_divider_color,),
                Container(
                  width: size.width,
                  height: 70,
                  child: ListView(
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    scrollDirection: Axis.horizontal,
                    children: _getDele(),
                  ),
                ),
                Divider(height: 0.5,color: APP_divider_color,),
                Expanded(
                  child: InkWell(
                    onTap: (){
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      alignment: Alignment.center,
                      child: Text("取消", style: instanceTextStyle(17, Colors.black),)),
                  ),
                )
              ],
            ),
          ),
        );
  }
}